package com.test.controller;


import com.test.entity.CompAllInfoDto;
import com.test.entity.RecruitDto;
import com.test.entity.StuAllInfoDto;
import com.test.entity.TimeTableDto;
import com.test.service.StuOperatService;
import com.test.service.TeaOperatService;
import com.test.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
@EnableScheduling
@RestController
@RequestMapping("/stuOperat")
public class StuOperatController extends BaseController{

    @Autowired
    private StuOperatService stuOperatService;



    @RequestMapping("/selectStuCode")
    @ResponseBody
    public String selectStuCode(String stu_code){
        String result= stuOperatService.selectStuCode(stu_code);
        return result;
    }

    @RequestMapping("/dataBangding")
    @ResponseBody
    public String dataBangding(String u_acount,String u_password,String wx_acount){
        String result= stuOperatService.dataBangding(u_acount,u_password,wx_acount);
        return result;
    }


    @RequestMapping("/delbangdingxitong")
    @ResponseBody
    public String delbangdingxitong(String wx_acount){
        String result= stuOperatService.delbangdingxitong(wx_acount);
        return result;
    }


    @RequestMapping("/scanLogin")
    @ResponseBody
    public String scanLogin(String wx_acount,HttpSession session){
        String result= stuOperatService.scanLogin(wx_acount,session);
        return result;
    }


    @RequestMapping("/selectStuByAcount")
    @ResponseBody
    public StuAllInfoDto selectStuByAcount(HttpSession session){
        String acount  = session.getAttribute("stu_u_acount").toString();
        StuAllInfoDto stuAllInfoDto = stuOperatService.selectStuByAcount(acount);
        String province = null;
        if(stuAllInfoDto.getStu_province()!=null&&!"".equals(stuAllInfoDto.getStu_province())){
           province = stuAllInfoDto.getStu_province();
        }
        String city = null;
        if(stuAllInfoDto.getStu_city()!=null&&!"".equals(stuAllInfoDto.getStu_city())){
            city = stuAllInfoDto.getStu_city();
        }
        String county = null;
        if(stuAllInfoDto.getStu_county()!=null&&!"".equals(stuAllInfoDto.getStu_county())){
            county = stuAllInfoDto.getStu_county();
        }
        String address = null;
        if(stuAllInfoDto.getStu_address()!=null&&!"".equals(stuAllInfoDto.getStu_address())){
            address = stuAllInfoDto.getStu_address();
        }

        String addresStr = province+city+county+address;
        stuAllInfoDto.setStu_address(addresStr);
        String fromProvince = null;
        if(stuAllInfoDto.getStu_fromprovince()!=null&&!"".equals(stuAllInfoDto.getStu_fromprovince())){
            fromProvince = stuAllInfoDto.getStu_fromprovince();
        }
        String fromCity = null;
        if(stuAllInfoDto.getStu_fromcity()!=null&&!"".equals(stuAllInfoDto.getStu_fromcity())){
            fromCity = stuAllInfoDto.getStu_fromcity();
        }

        String fromAddressStr = fromProvince+fromCity;
        stuAllInfoDto.setStu_fromaddress(fromAddressStr);
        if(stuAllInfoDto!=null&&!"".equals(stuAllInfoDto)){
            return stuAllInfoDto;
        }else{
            return null;
        }

    }

    @RequestMapping("/selectStuByAcountWx")
    @ResponseBody
    public StuAllInfoDto selectStuByAcountWx(String wx_acount){
        String acount  = stuOperatService.selectUacount(wx_acount);
        StuAllInfoDto stuAllInfoDto = stuOperatService.selectStuByAcount(acount);
        String province = null;
        if(stuAllInfoDto.getStu_province()!=null&&!"".equals(stuAllInfoDto.getStu_province())){
            province = stuAllInfoDto.getStu_province();
        }
        String city = null;
        if(stuAllInfoDto.getStu_city()!=null&&!"".equals(stuAllInfoDto.getStu_city())){
            city = stuAllInfoDto.getStu_city();
        }
        String county = null;
        if(stuAllInfoDto.getStu_county()!=null&&!"".equals(stuAllInfoDto.getStu_county())){
            county = stuAllInfoDto.getStu_county();
        }
        String address = null;
        if(stuAllInfoDto.getStu_address()!=null&&!"".equals(stuAllInfoDto.getStu_address())){
            address = stuAllInfoDto.getStu_address();
        }

        String addresStr = province+city+county+address;
        stuAllInfoDto.setStu_address(addresStr);
        String fromProvince = null;
        if(stuAllInfoDto.getStu_fromprovince()!=null&&!"".equals(stuAllInfoDto.getStu_fromprovince())){
            fromProvince = stuAllInfoDto.getStu_fromprovince();
        }
        String fromCity = null;
        if(stuAllInfoDto.getStu_fromcity()!=null&&!"".equals(stuAllInfoDto.getStu_fromcity())){
            fromCity = stuAllInfoDto.getStu_fromcity();
        }

        String fromAddressStr = fromProvince+fromCity;
        stuAllInfoDto.setStu_fromaddress(fromAddressStr);
        if(stuAllInfoDto!=null&&!"".equals(stuAllInfoDto)){
            return stuAllInfoDto;
        }else{
            return null;
        }

    }

    //提供给微信小程序的查询学生的接口
    @RequestMapping("/wxselectStuInfoByWx")
    @ResponseBody
    public StuAllInfoDto wxselectStuInfoByWx(String wx_acount){
        try {
            StuAllInfoDto stuAllInfoDto = stuOperatService.wxselectStuInfoByWx(wx_acount);
            String province = null;
            if (stuAllInfoDto.getStu_province() != null && !"".equals(stuAllInfoDto.getStu_province())) {
                province = stuAllInfoDto.getStu_province();
            }
            String city = null;
            if (stuAllInfoDto.getStu_city() != null && !"".equals(stuAllInfoDto.getStu_city())) {
                city = stuAllInfoDto.getStu_city();
            }
            String county = null;
            if (stuAllInfoDto.getStu_county() != null && !"".equals(stuAllInfoDto.getStu_county())) {
                county = stuAllInfoDto.getStu_county();
            }
            String address = null;
            if (stuAllInfoDto.getStu_address() != null && !"".equals(stuAllInfoDto.getStu_address())) {
                address = stuAllInfoDto.getStu_address();
            }

            String addresStr = province + city + county + address;
            stuAllInfoDto.setStu_address(addresStr);
            String fromProvince = null;
            if (stuAllInfoDto.getStu_fromprovince() != null && !"".equals(stuAllInfoDto.getStu_fromprovince())) {
                fromProvince = stuAllInfoDto.getStu_fromprovince();
            }
            String fromCity = null;
            if (stuAllInfoDto.getStu_fromcity() != null && !"".equals(stuAllInfoDto.getStu_fromcity())) {
                fromCity = stuAllInfoDto.getStu_fromcity();
            }

            String fromAddressStr = fromProvince + fromCity;
            stuAllInfoDto.setStu_fromaddress(fromAddressStr);
            if (stuAllInfoDto != null && !"".equals(stuAllInfoDto)) {
                return stuAllInfoDto;
            } else {
                return null;
            }
        }catch (Exception e){
            return null;
        }
    }


//提供给vue的查询学生的接口
    @RequestMapping("/selectStuByStuCode")
    @ResponseBody
    public StuAllInfoDto selectStuByStuCode(String stu_code){

        StuAllInfoDto stuAllInfoDto = stuOperatService.selectStuByStuCode(stu_code);
        String province = null;
        if(stuAllInfoDto.getStu_province()!=null&&!"".equals(stuAllInfoDto.getStu_province())){
            province = stuAllInfoDto.getStu_province();
        }
        String city = null;
        if(stuAllInfoDto.getStu_city()!=null&&!"".equals(stuAllInfoDto.getStu_city())){
            city = stuAllInfoDto.getStu_city();
        }
        String county = null;
        if(stuAllInfoDto.getStu_county()!=null&&!"".equals(stuAllInfoDto.getStu_county())){
            county = stuAllInfoDto.getStu_county();
        }
        String address = null;
        if(stuAllInfoDto.getStu_address()!=null&&!"".equals(stuAllInfoDto.getStu_address())){
            address = stuAllInfoDto.getStu_address();
        }

        String addresStr = province+city+county+address;
        stuAllInfoDto.setStu_address(addresStr);
        String fromProvince = null;
        if(stuAllInfoDto.getStu_fromprovince()!=null&&!"".equals(stuAllInfoDto.getStu_fromprovince())){
            fromProvince = stuAllInfoDto.getStu_fromprovince();
        }
        String fromCity = null;
        if(stuAllInfoDto.getStu_fromcity()!=null&&!"".equals(stuAllInfoDto.getStu_fromcity())){
            fromCity = stuAllInfoDto.getStu_fromcity();
        }

        String fromAddressStr = fromProvince+fromCity;
        stuAllInfoDto.setStu_fromaddress(fromAddressStr);
        if(stuAllInfoDto!=null&&!"".equals(stuAllInfoDto)){
            return stuAllInfoDto;
        }else{
            return null;
        }

    }




    //添加学生简历信息
    @RequestMapping("/insertStuResume")
    @ResponseBody
    public String  insertStuResume( HttpSession session,StuAllInfoDto stuAllInfoDto){
        String acount  = session.getAttribute("stu_u_acount").toString();
        stuAllInfoDto.setU_acount(acount);
        stuOperatService.deleteStuResume(acount);
        int result = stuOperatService.insertStuResume(stuAllInfoDto);
        if(result>0){
            return "已保存";
        }else{
            return "保存失败";
        }

    }

    //添加学生
    // 事件信息
    @RequestMapping("/insertTimeTableInfo")
    @ResponseBody
    public String  insertTimeTableInfo(HttpSession session, TimeTableDto timeTableDto){
        String acount  = session.getAttribute("stu_u_acount").toString();
        timeTableDto.setU_acount(acount);
        stuOperatService.deleteTimeTableInfo(acount);
        int result = stuOperatService.insertTimeTableInfo(timeTableDto);
        if(result>0){
            return "已保存";
        }else{
            return "保存失败";
        }

    }
    //查询学生
    // 事件信息
    @RequestMapping("/selectTimeTableInfo")
    @ResponseBody
    public TimeTableDto  selectTimeTableInfo(HttpSession session){
        String acount  = session.getAttribute("stu_u_acount").toString();
        TimeTableDto timeTableDto = stuOperatService.selectTimeTableInfo(acount);
        return timeTableDto;
    }


    //定时清空时间表
    @Scheduled(cron = "0 59 23 ? * SUN") //每周天晚上23:59:00秒删除时间表里面的数据
    public void  cleanTimeTableInfo(){
        stuOperatService.cleanTimeTableInfo();
    }


    //查看所有企业信息
    @RequestMapping("/selectAllComp")
    @ResponseBody
    public Result selectAllComp(CompAllInfoDto compAllInfoDto){
        Result result = genSuccessResult();
        try {
            List<CompAllInfoDto> CompList = stuOperatService.selectAllComp(compAllInfoDto);
            result .setData(CompList);
        }  catch (Exception e) {
            result = exceptionHandler.handleException(e);
        }
        return result;
    }

    //查看所有企业信息
    @RequestMapping("/selectCompByCount")
    @ResponseBody
    public Result selectCompByCount(String comp_acount){
        Result result = genSuccessResult();
        try {
            String arr[]=comp_acount.split(",");
            String compacount = arr[0];
            CompAllInfoDto compAllInfoDto = new CompAllInfoDto();
            compAllInfoDto.setU_acount(compacount);
            List<CompAllInfoDto> CompList = stuOperatService.selectAllComp(compAllInfoDto);
            result .setData(CompList);
        }  catch (Exception e) {
            result = exceptionHandler.handleException(e);
        }
        return result;
    }

    //查看所有门户招聘信息
    @RequestMapping("/selectAllMenHuInfo")
    @ResponseBody
    public Result selectAllMenHuInfo(RecruitDto recruitDto){
        Result result = genSuccessResult();
        try {
            List<RecruitDto> CompList = stuOperatService.selectAllMenHuInfo(recruitDto);
            result .setData(CompList);
        }  catch (Exception e) {
            result = exceptionHandler.handleException(e);
        }
        return result;
    }

    //学生收藏企业
    @RequestMapping("/stuAttentComp")
    @ResponseBody
    public String stuAttentComp(HttpSession session ,String comp_acount){
        String stu_acount  = session.getAttribute("stu_u_acount").toString();
            int result = stuOperatService.stuAttentComp(stu_acount,comp_acount);
            if(result==1){
                return "收藏成功";
            }else if(result==2){
                return "重复收藏";
            }else{
            return "收藏失败";
            }
    }

    //学生回复企业笔试预约
    @RequestMapping("/selectcompYuyueInfo")
    @ResponseBody
    public String selectcompYuyueInfo(HttpSession session ,String comp_acount,String wangshenjob){
        String stu_acount  = session.getAttribute("stu_u_acount").toString();
        int result = stuOperatService.selectcompYuyueInfo(stu_acount,comp_acount,wangshenjob);
        if(result>0){
            return "回复成功";
        }else{
            return "回复失败";
        }
    }

    //学生回复企业面试预约
    @RequestMapping("/selectcompYuyuemianInfo")
    @ResponseBody
    public String selectcompYuyuemianInfo(HttpSession session ,String comp_acount,String wangshenjob){
        String stu_acount  = session.getAttribute("stu_u_acount").toString();
        int result = stuOperatService.selectcompYuyuemianInfo(stu_acount,comp_acount,wangshenjob);
        if(result>0){
            return "回复成功";
        }else{
            return "回复失败";
        }
    }


    //小程序学生收藏企业
    @RequestMapping("/wxstufacomp")
    @ResponseBody
    public String wxstufacomp(String wx_acount,String  comp_acount){
        String stu_acount  = stuOperatService.selectUacount(wx_acount);
        if(stu_acount!=null&&!"".equals(stu_acount)){
            int result = stuOperatService.stuAttentComp(stu_acount,comp_acount);
            if(result==1){
                return "收藏成功";
            }else if(result==2){
                return "重复收藏";
            }else{
                return "收藏失败";
            }
        }else{
            return "账号未绑定";
        }

    }


    //小程序学生网申企业
    @RequestMapping("/wxstuattentcomp")
    @ResponseBody
    public String wxstuattentcomp(String wx_acount,String  comp_acount,String wangshenjob){

        String acount  = stuOperatService.selectUacount(wx_acount);
        try{
        if(acount!=null&&!"".equals(acount)) {
            StuAllInfoDto stuAllInfoDto = stuOperatService.selectStuByAcount(acount);
            if (stuAllInfoDto.getStu_name() == null || "".equals(stuAllInfoDto.getStu_name())) {
                return "信息不完善";
            } else if (stuAllInfoDto.getStu_tel() == null || "".equals(stuAllInfoDto.getStu_tel())) {
                return "信息不完善";
            } else if (stuAllInfoDto.getStu_resume_search() == null || "".equals(stuAllInfoDto.getStu_resume_search())) {
                return "信息不完善";
            } else if (stuAllInfoDto.getStu_idcard() == null || "".equals(stuAllInfoDto.getStu_idcard())) {
                return "信息不完善";
            } else if (stuAllInfoDto.getStu_resume_salary() == null || "".equals(stuAllInfoDto.getStu_resume_salary())) {
                return "信息不完善";
            } else if (stuAllInfoDto.getStu_resume_address() == null || "".equals(stuAllInfoDto.getStu_resume_address())) {
                return "信息不完善";
            } else if (stuAllInfoDto.getStu_resume_skills() == null || "".equals(stuAllInfoDto.getStu_resume_skills())) {
                return "信息不完善";
            } else {
                String compArr[] = comp_acount.split(",");
                String compAcount = compArr[0];
                int result = stuOperatService.stuOnlineApplication(acount, compAcount,wangshenjob);
                if (result == 1) {
                    return "网申成功";
                } else if (result == 2) {
                    return "重复网申";
                } else {
                    return "网申失败";
                }

            }
        }else{
            return "账号未绑定";
        }
        }catch (NullPointerException ne){
            return "信息不完善";
        }
    }


    //查询学生收藏
    @RequestMapping("/selectAttentAllComp")
    @ResponseBody
    public Result selectAttentAllComp(HttpSession session,CompAllInfoDto compAllInfoDto){
        String acount  = session.getAttribute("stu_u_acount").toString();
        Result result = genSuccessResult();
        try {
            List<CompAllInfoDto> CompList = stuOperatService.selectAttentAllComp(compAllInfoDto,acount);
            result .setData(CompList);
        }  catch (Exception e) {
            result = exceptionHandler.handleException(e);
        }
        return result;
    }



    //学生取消关注企业
    @RequestMapping("/delstuAttentComp")
    @ResponseBody
    public String delstuAttentComp(HttpSession session ,String comp_acount){
        String acount  = session.getAttribute("stu_u_acount").toString();
        int result = stuOperatService.delstuAttentComp(acount,comp_acount);
        if(result>0){
            return "取消成功";
        }else{
            return "取消失败";
        }
    }

  //学生网申企业
  @RequestMapping("/stuOnlineApplication")
  @ResponseBody
  public String stuOnlineApplication(HttpSession session ,String comp_acount,String wangshenjob){
      String acount  = session.getAttribute("stu_u_acount").toString();
      StuAllInfoDto stuAllInfoDto = stuOperatService.selectStuByAcount(acount);
      if(stuAllInfoDto.getStu_name()==null||"".equals(stuAllInfoDto.getStu_name())){
          return "信息不完善";
      }else if(stuAllInfoDto.getStu_tel()==null||"".equals(stuAllInfoDto.getStu_tel())){
          return "信息不完善";
      }else if(stuAllInfoDto.getStu_resume_search()==null||"".equals(stuAllInfoDto.getStu_resume_search())){
          return "信息不完善";
      }else if(stuAllInfoDto.getStu_idcard()==null||"".equals(stuAllInfoDto.getStu_idcard())){
          return "信息不完善";
      }else if(stuAllInfoDto.getStu_resume_salary()==null||"".equals(stuAllInfoDto.getStu_resume_salary())){
          return "信息不完善";
      }else if(stuAllInfoDto.getStu_resume_address()==null||"".equals(stuAllInfoDto.getStu_resume_address())){
          return "信息不完善";
      }else if(stuAllInfoDto.getStu_resume_skills()==null||"".equals(stuAllInfoDto.getStu_resume_skills())){
          return "信息不完善";
      }else{
          String compArr[] = comp_acount.split(",");
          String compAcount = compArr[0];
          int result = stuOperatService.stuOnlineApplication(acount,compAcount,wangshenjob);
          if(result==1){
              return "网申成功";
          }else if(result==2){
              return "重复网申";
          }else{
              return "网申失败";
          }

      }

  }



    @RequestMapping("/selectOnline")
    @ResponseBody
    public Result selectOnline(HttpSession session){
        String acount  = session.getAttribute("stu_u_acount").toString();
        Result result = genSuccessResult();
        try {
            List<CompAllInfoDto> CompList = stuOperatService.selectOnline(acount);
            result .setData(CompList);
        }  catch (Exception e) {
            result = exceptionHandler.handleException(e);
        }
        return result;
    }


}
